package com.egg.auth.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.auth.entity.user.User;
import com.egg.auth.enums.user.UserStateEnum;
import com.egg.auth.model.LoginUserInfo;
import com.egg.auth.model.request.web.UserPut;
import com.egg.auth.model.response.web.UserInfo;
import com.egg.auth.model.response.web.UserPage;
import com.egg.auth.service.login.LoginHelper;
import com.egg.auth.service.user.IUserService;
import com.egg.common.core.enums.ResponseCode;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.request.StateRequest;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.OptionalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.stream.Collectors;


@Slf4j
@Api(tags = WebController.API_PREFIX + "用户中心")
@RestController
@RequestMapping("/user")
public class WebUserController extends WebController {


    @Autowired
    private IUserService userService;


    @ApiOperation(value = "用户分页查询")
    @GetMapping("/page")
    public ResponseData<PageData<UserPage>> userPageGet(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @ApiParam(value = "是否启用") @RequestParam(value = "state", required = false) String state
            , @ApiParam(value = "在线") @RequestParam(value = "onlineState", required = false) Boolean onlineState
            , @ApiParam(value = "姓名/昵称/手机") @RequestParam(value = "search", required = false) String search
            , @ApiParam(value = "部门ID") @RequestParam(value = "deptId", required = false) Long deptId
    ) {
        Page<User> dbPage = userService.page(
                current, pageSize
                , OptionalUtil.build(state)
                , OptionalUtil.build(onlineState)
                , OptionalUtil.build(search)
                , OptionalUtil.build(deptId)
        );
        return ResponseData.success(new PageData<>(dbPage, userService.convertUserPage(dbPage.getRecords())));
    }

    @ApiOperation(value = "用户登录信息")
    @GetMapping("/login/info")
    public ResponseData<LoginUserInfo> currentUserInfoGet() {
        LoginUserInfo loginUserInfo = LoginHelper.getLoginUser();
        return ResponseData.success(loginUserInfo);
    }

    @ApiOperation(value = "指定用户详情")
    @GetMapping("/{id}/info")
    public ResponseData<UserInfo> userInfoGet(@ApiParam(value = "用户ID") @PathVariable String id) {
        return ResponseData.success(userService.buildUserInfoByUserId(id));
    }

    @ApiOperation(value = "用户修改")
    @PutMapping("/{id}")
    public ResponseData<String> userPut(
            @ApiParam(value = "用户ID", required = true) @PathVariable String id
            , @RequestBody UserPut userPut
    ) {
        userService.put(id, userPut);
        return ResponseData.success();
    }

    @ApiOperation(value = "用户启用/禁用")
    @PatchMapping("/{id}/state")
    public ResponseData<String> userStatePut(@PathVariable String id, @RequestBody StateRequest stateRequest) {
        if (UserStateEnum.isInvalid(stateRequest.getState())) {
            return ResponseData.error(ResponseCode.PARAM_INVALID);
        }
        User user = userService.baseGetById(id);
        User save = new User();
        save.setId(user.getId());
        save.setState(stateRequest.getState());
        userService.baseEdit(save);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "用户删除")
    @DeleteMapping
    public ResponseData<String> userDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        userService.delUser(
                ids.stream()
                        .filter(o -> ObjectUtil.isNotEmpty(o))
                        .map(o -> Long.valueOf(o))
                        .collect(Collectors.toSet())
        );
        return ResponseData.success();
    }


}
